package com.itisyue.fun.utils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.security.MessageDigest;
import java.util.Base64;

public class SecurityHelper {
    protected  final static Log logger = LogFactory.getLog(SecurityHelper.class);

    private final static int ITERATIONS = 20;

    public static String encrypt(String key, String plainText) throws Exception{
        String encryptTxt = "";
        try{
            byte[] salt = new byte[8];
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(key.getBytes());
            byte[] digest = md.digest();
            for( int i=0; i<8;i++){
                salt[i] = digest[i];
            }
            PBEKeySpec pbeKeySpec = new PBEKeySpec(key.toCharArray());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey skey = keyFactory.generateSecret(pbeKeySpec);
            PBEParameterSpec parameterSpec = new PBEParameterSpec(salt,ITERATIONS);
            Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
            cipher.init(Cipher.ENCRYPT_MODE, skey, parameterSpec);
            byte[] cipherText = cipher.doFinal(plainText.getBytes());
            String saltString = new String(Base64.getEncoder().encode(salt));
            String cipherTextString = new String(Base64.getEncoder().encode(cipherText));
            return saltString + cipherTextString;
        }catch (Exception e){
            throw new Exception("Encrypt Text Error" ,e);
        }
    }

    public static String decrypt(String key, String encrypyText) throws Exception{
        int saltLength = 12;
        try{
            String salt = encrypyText.substring(0, saltLength);
            String cipherText = encrypyText.substring(saltLength, encrypyText.length());
            byte[] saltArray = Base64.getDecoder().decode(salt.getBytes());
            byte[] cipherTextArray = Base64.getDecoder().decode(cipherText.getBytes());
            PBEKeySpec keySpec = new PBEKeySpec(key.toCharArray());
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
            SecretKey skey = keyFactory.generateSecret(keySpec);
            PBEParameterSpec parameterSpec = new PBEParameterSpec(saltArray,ITERATIONS);
            Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
            cipher.init(Cipher.ENCRYPT_MODE, skey, parameterSpec);
            byte[] plainTextArray = cipher.doFinal(cipherTextArray);
            return new String(plainTextArray);
        }catch (Exception e){
            throw  new Exception(e);
        }
    }

    public static void main(String[] args) throws Exception {
        byte[] encode = Base64.getEncoder().encode("/data/media/handWrite/2022/07/28/123456789012345.jpg".getBytes());
        String encrypt = encrypt("123wsa", new String(encode));//
        System.out.println(encrypt);

        String decrypt = decrypt("123wsa", encrypt);
        System.out.println(Base64.getEncoder().encode(decrypt.getBytes()));
    }

}
